/*
 * Created by JFormDesigner on Thu Jun 27 19:47:43 CST 2024
 */

package scct.zhanghongbo.ui;

import java.awt.event.*;

import scct.zhanghongbo.dao.StaffDao;
import scct.zhanghongbo.model.Staff;

import java.awt.*;
import java.util.Comparator;
import java.util.List;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author 84160
 */
public class StaffRegPanel extends JPanel {

    private static DefaultTableModel model;
    private static Staff staff;
    private StaffDao staffDao = new StaffDao();

    public StaffRegPanel(Staff staff) {
        StaffRegPanel.staff = staff;
        initComponents();
        model = (DefaultTableModel) tabUser.getModel();
        initView();
    }

    private void initView() {
        model.addColumn("编号");
        model.addColumn("员工名");
        model.addColumn("性别");
        model.addColumn("手机");
        model.addColumn("账号");
        model.addColumn("密码");
        model.addColumn("员工等级");
        model.addColumn("展馆编号");
        getTable();
    }

    public static void getTable() {
        model.setRowCount(0);
        List<Staff> list =new StaffDao().selectAll();
        getTableCore(list);
    }
    public static void getTable(List<Staff> list) {
        model.setRowCount(0);
        getTableCore(list);
    }

    private static void getTableCore(List<Staff> list) {
        list.sort(new Comparator<Staff>() {
            @Override
            public int compare(Staff o1, Staff o2) {
                return o1.getEhId()>o2.getEhId()?1:-1;
            }
        });
        for (Staff s : list) {
            model.addRow(new Object[]{
                    s.getId(),
                    s.getName(),
                    s.getGender(),
                    s.getPhone(),
                    s.getAccount(),
                    staff.getStaffRank()>s.getStaffRank()||staff.getId().equals(s.getId())?s.getPassword():"权限不足",
                    s.getStaffRank(),
                    s.getEhId()
            });
        }
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        DlgStaffAdd dlgStaffAdd = new DlgStaffAdd(staff,FormMain.MAIN);
        dlgStaffAdd.setModal(true);
        dlgStaffAdd.setVisible(true);
        getTable();
    }

    private void btnModify(ActionEvent e) {
        // TODO add your code here
        Integer id = getIdBySelect();
        if (id == null) return;
        DlgStaffModify dlgStaffModify = new DlgStaffModify(staff,FormMain.MAIN,id);
        dlgStaffModify.setModal(true);
        dlgStaffModify.setVisible(true);
        getTable();
    }

    private Integer getIdBySelect() {
        if (tabUser.getSelectedRowCount()==0) {
            JOptionPane.showMessageDialog(this,"请选择要修改的行");
            return null;
        }
        Integer index = tabUser.getSelectedRow();
        Integer id = Integer.parseInt(model.getValueAt(index, 0).toString());
        if (Integer.parseInt(model.getValueAt(index, 6).toString())>=staff.getStaffRank()) {
            JOptionPane.showMessageDialog(this,"权限不足");
            return null;
        }
        return id;
    }

    private void btnDelete(ActionEvent e) {
        // TODO add your code here
        Integer id = getIdBySelect();
        if (id == null) return;
        if (staffDao.deleteRowById(id)) JOptionPane.showMessageDialog(this,"删除成功");
        else JOptionPane.showMessageDialog(this,"删除失败");
        getTable();
    }

    private void btnSearch(ActionEvent e) {
        // TODO add your code here
        String[] arr = new String[] {"id","name","gender","phone","account","ehId"};
        Integer index = comboBox.getSelectedIndex();
        String value = txtSearch.getText();
        System.out.println(arr[index]+value);
        List<Staff> list = staffDao.search(arr[index], value);
        System.out.println(list);
        getTable(list);
    }

    private void txtSearchKeyPressed(KeyEvent e) {
        // TODO add your code here
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            btnSearch.doClick();
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        scrollPane1 = new JScrollPane();
        tabUser = new JTable();
        btnPanel = new JPanel();
        btnAdd = new JButton();
        btnModify = new JButton();
        btnDelete = new JButton();
        searchPanel = new JPanel();
        comboBox = new JComboBox<>();
        txtSearch = new JTextField();
        btnSearch = new JButton();

        //======== this ========
        setLayout(new BorderLayout());

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(tabUser);
        }
        add(scrollPane1, BorderLayout.CENTER);

        //======== btnPanel ========
        {
            btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

            //---- btnAdd ----
            btnAdd.setText("\u65b0\u589e");
            btnAdd.addActionListener(e -> btnAdd(e));
            btnPanel.add(btnAdd);

            //---- btnModify ----
            btnModify.setText("\u4fee\u6539");
            btnModify.addActionListener(e -> btnModify(e));
            btnPanel.add(btnModify);

            //---- btnDelete ----
            btnDelete.setText("\u5220\u9664");
            btnDelete.addActionListener(e -> btnDelete(e));
            btnPanel.add(btnDelete);
        }
        add(btnPanel, BorderLayout.NORTH);

        //======== searchPanel ========
        {
            searchPanel.setLayout(new BorderLayout());

            //---- comboBox ----
            comboBox.setModel(new DefaultComboBoxModel<>(new String[] {
                "\u7f16\u53f7",
                "\u5458\u5de5\u540d",
                "\u6027\u522b",
                "\u624b\u673a\u53f7",
                "\u8d26\u53f7",
                "\u5c55\u9986\u7f16\u53f7"
            }));
            searchPanel.add(comboBox, BorderLayout.WEST);

            //---- txtSearch ----
            txtSearch.addKeyListener(new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent e) {
                    txtSearchKeyPressed(e);
                }
            });
            searchPanel.add(txtSearch, BorderLayout.CENTER);

            //---- btnSearch ----
            btnSearch.setText("\u67e5\u8be2");
            btnSearch.addActionListener(e -> btnSearch(e));
            searchPanel.add(btnSearch, BorderLayout.EAST);
        }
        add(searchPanel, BorderLayout.SOUTH);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JScrollPane scrollPane1;
    private JTable tabUser;
    private JPanel btnPanel;
    private JButton btnAdd;
    private JButton btnModify;
    private JButton btnDelete;
    private JPanel searchPanel;
    private JComboBox<String> comboBox;
    private JTextField txtSearch;
    private JButton btnSearch;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
